//查询数据
function getData(url) {
    let flag = new Array();
    $.ajax({
            type: 'POST',
            async: false,
            url: preUrl + url,
            data: {
                pageSize: 100000,
            }
        })
        .done(function(res) {
            if (res.code == 100) {

                flag = res.object;
            } else {
                layer.msg(res.extend.msg);
                flag = false;
            }
        })
    return flag;
}



function checkPage(i, obj) {
    for (let j in obj) {
        if (i == j) {
            return obj[j];
        }
    }
}


//返回当前页字典对象
function checkCurPage() {
    var keyStr = window.location.href.split("home/")[1].split(".html")[0];
    switch (keyStr) {
        case "product":
            return productKey;
            break;
        default:
            break;
    };

}


//翻译当前页字段含义
function translateName(i) {
    var keyStr = window.location.href.split("home/")[1].split(".html")[0];
    switch (keyStr) {
        case "product":
            return checkPage(i, productKey);
            break;
        default:
            break;
    };

}

function returnFormText(fName, type) {
    switch (type) {
        case "text":
            return "<div class='layui-form-item'><label class='layui-form-label'>" + translateName(fName) + "</label><div class='layui-input-block'><input type='text' name='" + fName + "'  lay-verify='required' autocomplete='off' placeholder='请输入' class='layui-input'></div></div>";
            break;
        case "radio":
            return "<div class='layui-form-item'><label class='layui-form-label'>" + translateName(fName) + "</label><div class='layui-input-block'><input type='radio' name='" + fName + "'  value='1' title='是'  ><input type='radio' name='" + fName + "' value='0' title='否' checked=''> </div></div>";
            break;
        case "file":
            return "<div class='layui-form-item'><label class='layui-form-label'>" + translateName(fName) + "</label><div class='layui-input-block'><div class='layui-btn layui-btn-normal'>上传" + translateName(fName) + "</div><input type='file' name='"+fName+"' class='uploadBtn' /></div></div>";
            break;
        default:
            break;
    }
}

function returnFormType(fName) {
    if (TextKey.indexOf(fName) > -1) {
        return returnFormText(fName, "text");
    } else if (CheckboxKey.indexOf(fName) > -1) {
        return returnFormText(fName, "radio");
    } else if (FileKey.indexOf(fName) > -1) {
            return returnFormText(fName, "file");
        }
    }
   
    function add(url) {
        $(".addForm").html("");
       // var cont = "<div class='layui-form addForm'>";
        for (let i in checkCurPage()) {
              $(".addForm").append($(returnFormType(i)));
        }
       // cont+="</div>"
        $(".addForm").css("padding-top", "30px");
        var submit = $("<div class='layui-form-item'><label class='layui-form-label'></label><div class='layui-input-block'><button class='layui-btn' lay-submit='' lay-filter='formDemo'>立即提交</button><button type='reset' class='layui-btn layui-btn-primary'>重置</button></div></div>");
        $(".addForm").append(submit);
        $(".addForm").show();
        // var index = layer.open({
        //     type:1,
        //     area:['80%', '100%'],
        //     title:"新增数据",
        //     content:$(".addForm"),
        // });

        layui.use(['table', 'form', 'upload'], function() {
            var form = layui.form;
            var upload = layui.upload;
            var table=layui.table;
         
            //监听提交
             form.on('submit(formDemo)', function(data) {
                 
                  $(".addForm").hide();
                var formData = new FormData();
               
                for (let j in data.field) {
                      formData.append(j, data.field[j]);
                }
                $.ajax({
                        type: 'POST',
                        async: false,
                        url: preUrl + url,
                        contentType: false,
                        processData: false,
                        data: formData
                    })
                    .done(function(res) {
                        if (res.code == 100) {

                       
                            layer.msg("添加成功");
                           // table.render();
                        } else {
                            layer.msg(res.extend.msg);

                        }
                    })
                 
            });
            var table = layui.table
            form.render();

        })



    }

    function initTable(url) {
        let data = getData(url);
        if (!data) {
            return false;
        }
        let newArr = new Array();
        let preObj = {
            fixed: 'left',
            type: 'checkbox'
        };
        let laterObj = {
            fixed: 'right',
            title: '操作',
            toolbar: '#barDemo'
        };


        newArr.push(preObj);
        for (let i in data[0]) {
            let objItem = new Object();
            objItem.field = i;
            objItem.title = translateName(i);
            objItem.sort = true;
            objItem.edit = "text";
            if (i.indexOf('Id') > -1) {
                objItem.title = i;
                delete objItem.edit;

            }

            newArr.push(objItem);
        }
        newArr.push(laterObj);
        var finalArr = new Array();
        finalArr.push(newArr);
        layui.use(['table', 'form'], function() {
            var form = layui.form;
            var table = layui.table;
            layui.table.render({
                elem: '#test',
                toolbar: '#mytoolbar',
                cellMinWidth: 170,
                url:"http://47.105.125.95:8080/testWeb/product/list",
                method:"POST",
                title: '用户数据表',
                  request:{
                     limitName:'pageSize',
                 },
                 response: {
                 statusName: 'code',
                 statusCode:100 ,
                 dataName: 'object'  
                },
                page: true,
                limit: 10,
                cols: finalArr,
             //   data: data

            });


            //监听行工具事件
            table.on('tool()', function(obj) {
                console.log(obj)
                var data = obj.data;

                if (obj.event === 'del') {
                    layer.confirm('真的删除么', function(index) {
                        obj.del();
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    layer.msg("可直接在单元格里修改..");
                } else if (obj.event == 'detail') {
                    layer.msg("11")
                }
            });
        });


        // //监听单元格编辑
        // table.on('edit', function(obj) {
        //     console.log(obj)
        //     var value = obj.value,
        //         data = obj.data,
        //         field = obj.field; //得到字段
        //     layer.msg(field + ' 字段更改为：' + value);
        // });
        // table.on('addData', function(obj) {
        //    alert(obj)
        //     var value = obj.value,
        //         data = obj.data,
        //         field = obj.field; //得到字段
        //     layer.msg(field + ' 字段更改为：' + value);
        // });






    }